package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.kingyun.gpsinspection.purificationservice.common.constant.BackMessage;
import com.kingyun.gpsinspection.purificationservice.common.enums.FileTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomFileMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.OrganizationFileMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.OrganizationFile;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.OrgFileService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资料查询service实现类
 * Created by yangcs on 2017/4/1.
 */
public class OrgFileServiceImpl implements OrgFileService {

    @Autowired
    private OrganizationFileMapper organizationFileMapper;
    @Autowired
    private SomFileMapper somFileMapper;

    /**
     * 获取列表数据
     * @param organizationFile
     * @return
     */
    @Override
    public JSONObject list(OrganizationFile organizationFile) {
        JSONObject jsonObject = new JSONObject();
        organizationFile.setLimit(organizationFile.getLimit() == null ? 10 : organizationFile.getLimit());
        organizationFile.setOffset(organizationFile.getOffset() == null ? 0 : organizationFile.getOffset());
        jsonObject.put("total", organizationFileMapper.queryListCount(organizationFile));
        jsonObject.put("rows", JSON.toJSON(organizationFileMapper.queryList(organizationFile)));
        return jsonObject;
    }

    /**
     * 新增
     * @param organizationFile
     * @return
     */
    @Override
    public int insertOrgFile(OrganizationFile organizationFile,String uploadFiles,String deletedFiles) {
        organizationFileMapper.insert(organizationFile);
        int result = saveFiles(uploadFiles,organizationFile.getOrganizationFileId());
        return result;
    }

    /**
     * 批量存储图片
     * @param uploadFiles
     * @return
     */
    public int saveFiles(String uploadFiles,String businessId){
        int result = 0;
        if(uploadFiles!=null && !uploadFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                //重新设置附件属性
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(businessId);
                    _file.setFileType(FileTypeEnum.getCode(_file.getExt()));
                    result += somFileMapper.insert(_file);
                }
            }
        }
        return result;
    }

    /**
     * 修改
     * @param organizationFile
     * @return
     */
    @Override
    public int updateOrgFile(OrganizationFile organizationFile,String uploadFiles,String deletedFiles) {
        organizationFileMapper.updateByPrimaryKey(organizationFile);
        //更新附件信息
        int result = saveFiles(uploadFiles,organizationFile.getOrganizationFileId());
        //需要删除的附件信息
        if(deletedFiles!=null && !deletedFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(deletedFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    somFileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }
        return result;
    }

    /**
     * 删除
     * @param list
     * @return
     */
    @Override
    public Map<String,Object> deleteOrgFile(List<String> list) {
        Map<String, Object> resMap = new HashMap<>();
        if(list!=null){
            organizationFileMapper.updateStatus(list);
            resMap.put("msg", BackMessage.DEL_OK);
            resMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
        }else{
            resMap.put("msg",BackMessage.EXCEPTION_ISNULL);
            resMap.put("state",RespCodeEnum.RESP_CODE_ENUM_false.code());
        }
        return resMap;
    }

    /**
     * 查询
     * @param organizationFileId
     * @return
     */
    @Override
    public OrganizationFile queryById(String organizationFileId) {
        OrganizationFile organizationFile = organizationFileMapper.queryById(organizationFileId);
        List<File> list = somFileMapper.findById(organizationFile.getOrganizationFileId());
        if( list != null && list.size() > 0 ){
            organizationFile.setFilesData( JSON.toJSONString(list) );
        }
        return organizationFile;
    }

    /**
     * 查询图片
     * @param limit
     * @param offset
     * @return
     */
    @Override
    public List<File> queryOrgFiles(String orgId,int limit, int offset, int orgFileType) {
        Map map = new HashMap<>();
        map.put("orgId",orgId);
        map.put("limit",limit);
        map.put("offset",offset);
        map.put("orgFileType",orgFileType);
        return somFileMapper.queryOrgFile(map);
    }
}
